﻿using System;
using System.Configuration;
using System.Web.Security;
using System.Data.SqlClient;
using System.Data;

namespace ImgForecast.admin
{
    public partial class AddManager : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }



        protected void Submit_Click(object sender, EventArgs e)
        {
            MembershipUser newUser = Membership.CreateUser(UsernameTextBox.Text, "password", "ffinstad@gmail.com");
            Guid newUserId = (Guid)newUser.ProviderUserKey;

            Roles.AddUserToRole(newUser.UserName, Role.SelectedValue);
       

            // Insert a new record into UserProfiles
            string connectionString =
                 ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString;
            string insertSql = "INSERT INTO UserProfiles(UserId, Firstname, Lastname, OfficeID, DivisionID) VALUES(@UserId, @Firstname, @Lastname, @Office, @Division)";

            using (SqlConnection myConnection = new SqlConnection(connectionString))
            {
                try
                {
                    myConnection.Open();
                    SqlCommand myCommand = new SqlCommand(insertSql, myConnection);
                    myCommand.Parameters.Add("@UserId", SqlDbType.UniqueIdentifier);
                    myCommand.Parameters["@UserId"].Value = newUserId;
                    myCommand.Parameters.AddWithValue("@Firstname", FirstnameTextBox.Text);
                    myCommand.Parameters.AddWithValue("@Lastname", LastnameTextBox.Text);
                    myCommand.Parameters.AddWithValue("@Office", Office.SelectedValue);
                    myCommand.Parameters.AddWithValue("@Division", Division.SelectedValue);
                    myCommand.ExecuteNonQuery();
                    myConnection.Close();
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }

            Response.Redirect("/admin/Managers.aspx");
        }



    }
}